FROM python:3.11-slim AS builder

# Deployment environment: production|local
ARG DEPLOYMENT_ENVIRONMENT=production

# Private Tetrascience PyPI repository details
ARG TS_PYPI_VIRTUAL_USER
# The token is passed in as the secret TS_PYPI_VIRTUAL_TOKEN

WORKDIR /usr/src/app

RUN apt-get update && apt-get install -y --no-install-recommends \
        gcc \
        g++ \
        make \
    && pip install --upgrade pip \
    && pip install --upgrade setuptools poetry \
    && poetry config virtualenvs.create false

# Install python dependencies (aggressively cleanup afterwards to reduce final image size by ~40MB)
COPY pyproject.toml poetry.lock ./

RUN --mount=type=secret,id=TS_PYPI_VIRTUAL_TOKEN \
    POETRY_HTTP_BASIC_TS_PYPI_VIRTUAL_USERNAME=${TS_PYPI_VIRTUAL_USER} \
    POETRY_HTTP_BASIC_TS_PYPI_VIRTUAL_PASSWORD=$(cat /run/secrets/TS_PYPI_VIRTUAL_TOKEN) \
    poetry install --only main --no-root --no-cache \
    && poetry cache clear --all pypi

RUN find /usr/local/lib/python3.11/site-packages -type f -name "*.pyc" -delete \
    && find /usr/local/lib/python3.11/site-packages -type f -name "*.pyo" -delete \
    && find /usr/local/lib/python3.11/site-packages -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true \
    && find /usr/local/lib/python3.11/site-packages -type d -name "tests" -exec rm -rf {} + 2>/dev/null || true \
    && find /usr/local/lib/python3.11/site-packages -type d -name "test" -exec rm -rf {} + 2>/dev/null || true \
    && find /usr/local/lib/python3.11/site-packages -type d -name "*.dist-info" -exec find {} -name "RECORD" -delete \; \
    && find /usr/local/lib/python3.11/site-packages -name "*.so" -exec strip {} \; 2>/dev/null || true \
    && pip cache purge \
    && rm -rf /root/.cache /tmp/* /var/cache/apt/* /var/lib/apt/lists/* \
    && apt-get purge -y --auto-remove gcc g++ make


FROM python:3.11-slim

# Redeclare the ARG for the second stage
ARG DEPLOYMENT_ENVIRONMENT=production

WORKDIR /app

# Install supervisor
RUN apt-get update && apt-get install -y --no-install-recommends \
        supervisor \
    && rm -rf /var/lib/apt/lists/*

COPY images ./images/
# Install the python app (this is done after the dependencies to avoid re-installing them
# on every change in the app code)
COPY {{ cookiecutter.data_app._package_dir }} {{ cookiecutter.data_app._package_dir }}
COPY heartbeat.py app.py ./
COPY .streamlit/config-docker.toml .streamlit/config.toml

# Copy Python packages and executables from builder stage
COPY --from=builder /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages
COPY --from=builder /usr/local/bin /usr/local/bin

# Conditionally copy files depending on the deployment environment
COPY conf/ /tmp/conf/
RUN mkdir -p /etc/supervisor/conf.d \
    && if [ "$DEPLOYMENT_ENVIRONMENT" = "production" ]; then \
        cp /tmp/conf/supervisord.conf /etc/supervisor/conf.d/supervisord.conf; \
    elif [ "$DEPLOYMENT_ENVIRONMENT" = "local" ]; then \
        cp /tmp/conf/supervisord-local.conf /etc/supervisor/conf.d/supervisord.conf; \
    else \
        echo "Invalid DEPLOYMENT_ENVIRONMENT: $DEPLOYMENT_ENVIRONMENT"; \
        exit 1; \
    fi \
    && rm -rf /tmp/conf/

# Cleanup to remove unnecessary files from final image
RUN rm -rf /var/lib/apt/lists/* \
    && rm -rf /tmp/* \
    && find /usr/local -name "*.a" -delete \
    && find /app -name "*.pyc" -delete \
    && find /app -name "__pycache__" -type d -exec rm -rf {} + 2>/dev/null || true

CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
